package com.dominator.timer;

import com.dominator.mybatis.dao.T_carDao;
import com.dominator.mybatis.dao.T_carPO;
import com.dominator.utils.dao.RedisUtil;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.impl.HashDto;
import com.dominFramework.core.utils.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Random;

/**
 * 基于注解的定时器
 */
@Component
public class PushCarStatus {

    private static final Logger log = LoggerFactory.getLogger(PushCarStatus.class);

    private static RedisUtil ru = RedisUtil.getRu();

    @Autowired
    private T_carDao t_carDao;

    /**
     * 心跳更新。启动时执行一次，之后每隔2秒执行一次
     */
//    @Scheduled(fixedRate = 1000 * 5)
    public void PushCarStatus() {
        log.info("PushCarStatus start");
        Dto dto = new HashDto();
        dto.put("is_valid", "1");
        List<T_carPO> list = t_carDao.list(dto);
//        log.info("carlist:{}", list.toString());
        if (!SystemUtils.isEmpty(list) && list.size() > 0) {
            for (T_carPO car : list) {
                int sydl_last_time = 100;
                int sydl = 100;
                String pzzt_last_time = "0";
                String pzzt = "0"; //0未破损，1已破损
                //调用五菱接口，查询车辆状态

                //模拟
                Random random = new Random();
                sydl = random.nextInt(100);
                pzzt = String.valueOf(random.nextInt(2));
                //将符合条件的信息，推送给用户
                if (sydl < 30) {
                    if(!SystemUtils.isEmpty(ru.get("car_sydl_" + car.getCar_vin()))) {
                        sydl_last_time = Integer.valueOf(ru.get("car_sydl_" + car.getCar_vin()));
                    }
                    log.info("sydl_last_time:{}", sydl_last_time);
                    log.info("sydl:{}", sydl);
                    if (sydl_last_time / 10 > sydl / 10) {
                        String message = "------剩余电量不足";
                        String message1 = "";
                        if (sydl >= 20) {
                            message1 = "30%";
//                            log.info("------{}剩余电量不足30%", car.getCar_vin());
                        }else if(sydl >= 10){
                            message1 = "20%";
//                            log.info("------{}剩余电量不足20%", car.getCar_vin());
                        }else {
                            message1 = "10%";
//                            log.info("------{}剩余电量不足10%", car.getCar_vin());
                        }
                        //推送用户，电量不足
                        message += message1;
                        log.info(car.getCar_vin() + message);
                    }
                }
                if(pzzt.equals("1")) {
                    if(!SystemUtils.isEmpty(ru.get("car_pzzt_" + car.getCar_vin()))) {
                        pzzt_last_time = ru.get("car_pzzt_" + car.getCar_vin());
                    }
                    log.info("pzzt_last_time:{}", pzzt_last_time);
                    log.info("pzzt:{}", pzzt);
                    if(pzzt_last_time.equals("0")){
                        log.info("------{}车辆破损", car.getCar_vin());
                        //推送用户，车辆破损
                    }
                }

                //本次状态存redis
                ru.set("car_sydl_" + car.getCar_vin(), String.valueOf(sydl));
                ru.set("car_pzzt_" + car.getCar_vin(), pzzt);
            }
        }
        log.info("PushCarStatus end");
    }
}
